﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;


#if !NETCOREAPP
using System.Web;
using System.Web.Mvc;
using System.Web.WebPages;
    using System.Web.Mvc.Html;

#endif

#if NETCOREAPP
using Microsoft.AspNetCore.Mvc.Rendering;
using Kugar.Core.Web;
#endif

using Kugar.Core.ExtMethod;
using Kugar.MVC.MobileUI.WeUI.Enum;
using Kugar.MVC.MobileUI.WeUI.Helpers;
using Kugar.MVC.MobileUI.WeUI.PageControls.PageCommons;

namespace Kugar.MVC.MobileUI.WeUI.PageControls.PageForm
{

    public abstract class PageForm_CellForField:ControlBase
    {
        //internal bool _swiped = false;
        private string _iconUrl = "";
        //internal Lazy<List<SwipeButton>> _swipeLst = new Lazy<List<SwipeButton>>(() => new List<SwipeButton>(2));
        

        protected PageForm_CellForField(ViewContext viewContext) : base(viewContext)
        {
        }

        protected virtual void RenderHeader() { }

        protected virtual void RenderBody() { }

        protected virtual void RenderFooter()
        {
        }

        protected virtual void RenderTips()
        {
            
        }

        //protected virtual void RenderSwipedBegin()
        //{
        //    if (_swiped)
        //    {
        //        ViewContext.Writer.WriteLine("<div class=\"weui-cell weui-cell_swiped\">");

        //        ViewContext.Writer.WriteLine("<div class=\"weui-cell__bd\">");
        //    }
        //}

        //protected virtual void RenderSwipeEnd()
        //{
        //    if (_swiped)
        //    {
        //        ViewContext.Writer.WriteLine("</div> <!-- end swiped cell__bd -->");

        //        ViewContext.Writer.WriteLine("<div class=\"weui-cell__ft\">");

        //        foreach (var btn in _swipeLst.Value)
        //        {
        //            ViewContext.Writer.WriteLine(btn.RenderButton());
        //        }

        //        ViewContext.Writer.WriteLine("</div> <!-- end swiped cell__ft -->");

        //        ViewContext.Writer.WriteLine("</div> <!-- end cell_swiped  -->");
        //    }
        //}

        public override MvcHtmlString Render()
        {
            //RenderSwipedBegin();

            RenderHeader();

            RenderBody();

            RenderFooter();

            //RenderSwipeEnd();

            RenderTips();

            return MvcHtmlString.Empty;
        }

        
        
    }

    public abstract class PageForm_FieldBase: PageForm_CellForField
    {
        internal string _name = "";
        internal string _label = "";
        internal string _buttomTips = "";
        internal string _tip = "";
        internal ButtonItemBase _moreBtn = null;
        

        public PageForm_FieldBase(ViewContext viewContext) : base(viewContext)
        {
            _attrs.Class("mobileui-form-field weui-cell");
        }

        protected string getName() => _name;

        protected string getLabel() => _label;

        protected virtual void BeginRender()
        {

        }

        protected virtual void RenderHeader()
        {
            ViewContext.Writer.WriteLine($"<label class=\"weui-label\">{_label}</label>");
        }

        public override MvcHtmlString Render()
        {
            /*
             <div class="weui-cell">
                <div class="weui-cell__hd"><label class="weui-label">qq</label></div>
                <div class="weui-cell__bd">
                  <input class="weui-input" type="number" pattern="[0-9]*" placeholder="请输入qq号">
                </div>
              </div>
             */

            

            if (Html.ViewData.ModelState.ContainsKey(_name))
            {
                _attrs.Class("weui-cell_warn");
            }

            //if (_swipeLst.IsValueCreated)
            //{
            //    this.Css("weui-cell_swiped");
            //}

            BeginRender();

   
            //ViewContext.Writer.WriteLine($"<div {_attrs} >");

            //    ViewContext.Writer.WriteLine("<div class=\"weui-cell__bd\">");

                    ViewContext.Writer.WriteLine($"<div {_attrs}>");

                        ViewContext.Writer.WriteLine($"<div class=\"weui-cell__hd\">");

                            RenderHeader();

                        ViewContext.Writer.WriteLine("</div>");
            
                        ViewContext.Writer.WriteLine($"<div class=\"weui-cell__bd\">");

                            RenderBody();

                        ViewContext.Writer.WriteLine("</div> <!-- End cell__bd -->");

                        ViewContext.Writer.WriteLine("<div class=\"weui-cell__ft\">");

                            RenderFooter();

                            ViewContext.Writer.WriteLine($"<i class=\"weui-icon-warn\" data-error=\"{ErrorMessage}\"></i>");
            
                        ViewContext.Writer.WriteLine("</div> <!-- end cell__ft -->");

                    ViewContext.Writer.WriteLine("</div> <!-- End weui-cell Field -->");

            if (_moreBtn!=null)
            {
                ViewContext.Writer.WriteLine(Utility.ButtonConfigToHtml(_moreBtn,"a"));
            }
                    

               // ViewContext.Writer.WriteLine("</div> <!-- end  weui-cell__bd -->");

            

            //if (_swipeLst.IsValueCreated)
            //{
            //    ViewContext.Writer.WriteLine("<div class=\"weui-cell__ft\">");

            //    foreach (var swipe in _swipeLst.Value)
            //    {
            //        ViewContext.Writer.WriteLine($"<a class=\"weui-swiped-btn {styleToString(swipe._styleType)} {swipe._css}\" {swipe._data}  onclick=\"{swipe._clickScript}\" href=\"javascript:\">{swipe._text}</a>");
            //    }

            //    ViewContext.Writer.WriteLine("</div> <!-- end weui-cell__ft -->");
            //}
            
            //ViewContext.Writer.WriteLine("</div> <!-- end weui-cell -->");

            RenderTips();

            return MvcHtmlString.Empty;
        }

        protected override void RenderTips()
        {
            base.RenderTips();

            if (!string.IsNullOrWhiteSpace(_tip))
            {
                ViewContext.Writer.WriteLine($"<div class=\"weui-cells__tips\">{_tip}</div>");
            }
        }

        public bool HasError
        {
            get
            {
                if (!string.IsNullOrWhiteSpace(_name) && Html.ViewData.ModelState.ContainsKey(_name))
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
        }

        public string ErrorMessage
        {
            get
            {
                if (!HasError)
                {
                    return String.Empty;
                }

                var state = Html.ViewData.ModelState[_name];

                var error =state.Errors.Select(x => x.ErrorMessage).JoinToString(' ');
                
                //ModelMetadata metadata = ModelMetadata.FromStringExpression(_name, ViewContext.ViewData);

                //var error = string.Join("&nbsp&nbsp",
                //    Html.GetUnobtrusiveValidationAttributes(_name, metadata)
                //        .Select(x => x.Key.ToString() + "=\"" + x.Value + "\""));

                return HttpUtility.HtmlEncode(error);
            }
        }

        private string styleToString(ButtonStyleType type)
        {
            switch (type)
            {
                case ButtonStyleType.Default:
                    return "weui-swiped-btn_default";
                case ButtonStyleType.Primary:
                    break;
                case ButtonStyleType.Success:
                    break;
                case ButtonStyleType.Warning:
                    return "weui-swiped-btn_warn";
                case ButtonStyleType.Danger:
                    break;
                case ButtonStyleType.Lazur:
                    break;
                default:
                    throw new ArgumentOutOfRangeException(nameof(type), type, null);
            }

            return "";
        }
    }
    
}